﻿using System;
using System.ComponentModel;

namespace RG.Common.WPF.Filters
{
    
    [SupportDataBinding]
    public class FiltersFrameVM
    {
        protected BindingList<FilterBaseVM> filter = new BindingList<FilterBaseVM>();
        public BindingList<FilterBaseVM> Filter { get { return filter; } }

        public int SelectedIndex { get; set; }

        public void Add(FilterBaseVM item)
        {
            filter.Add(item);
        }

        public T Get<T>(string id) where T : FilterBaseVM
        {
            foreach (var item in filter)
                if (item.Id == id)
                    return (T)item;

            throw new ApplicationException(string.Format("Не нашли фильтр '{0}'", id));
        }

        public void ClearCurrentFilter()
        {
            if ((SelectedIndex >=0) && (SelectedIndex < Filter.Count))
                Filter[SelectedIndex].ClearValue();
        }
    }
}
